package com.hotel.wallet.controller;


import cn.hutool.core.convert.NumberWithFormat;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hotel.common.config.SystemConfig;
import com.hotel.common.util.JwtUtil;
import com.hotel.common.vo.R;
import com.hotel.user.entity.PayLog;
import com.hotel.wallet.mapper.PayLogMapper;
import com.hotel.wallet.service.PayLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 支付宝交易日志表 前端控制器
 * </p>
 *
 * @author zhy
 * @since 2024-06-17
 */
@RestController
@RequestMapping("/pay")
public class PayLogController {

    @Autowired
    private PayLogService payLogService;

    @Autowired
    private PayLogMapper payLogMapper;

    /**
     * 支付宝支付成功后回调
     */
    @RequestMapping("/alipay/cb")
    public String alipayCallBack(@RequestParam("out_trade_no") String no){

        payLogService.alipayCallback(no);

        return "redirect:http://localhost:5173/wallet";

    }
    /**
     * 消费记录
     */
    @RequestMapping("/payLogs")
    public R findPayLogs(HttpServletRequest request) {

        // 获取id
        // 前端没有传id，通过token得到id需要HttpServletRequest
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        // 解析token得到id(token是由user.getId(), user.getAccount(), user.getPhone()组成)
        Integer id = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();

        QueryWrapper<PayLog> payLogQueryWrapper = new QueryWrapper<>();
        payLogQueryWrapper.eq("user_id",id);

        List<PayLog> payLogsList = payLogMapper.selectList(payLogQueryWrapper);

        if (payLogsList == null) {

            return R.fail("未查询到数据");
        }

        return R.ok(payLogsList);
    }

}

